//解析字符串
let cnCode = ['零', '壹', '貳', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾'];
let opCode = ['加', '减'];

/**
 * 计算函数
 * @param str string字符串 大写中文计算字符串
 * @return int整型
 */
function calculate(str) {
    //转换成数组
    let arr = str.split('');

    let queue = []; //计算队列
    let num = []; //数字
    //2.找数字 加减入栈
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] == '加' || arr[i] == '减') {
            //是操作符时
            if (num.length > 1) {
                let n = num[0] * 10;
                if (num.length === 3) {
                    n += num[2];
                }
                queue.push(n);
                num = [];
            } else {
                queue.push(arr[0]);
            }
            if (arr[i] === '加') {
                queue.push('+');
            }
            if (arr[i] === '减') {
                queue.push('-');
            }
        } else {
            if (arr[i] === '拾') {
                num.push(0);
            } else {
                num.push(cnCode.indexOf(arr[i]));
            }
        }
        if (i == arr.length - 1) {
            if (num.length > 1) {
                let n = num[0] * 10;
                if (num.length === 3) {
                    n += num[2];
                }
                queue.push(n);
                num = [];
            } else {
                queue.push(num[0]);
            }
        }
    }
    // console.log(queue);
    // 计算
    let sum = queue[0];
    for (let i = 1; i < queue.length; i = i + 2) {
        console.log(queue[i], queue[i + i]);
        if (queue[i] == '+') {
            sum = sum + queue[i + 1];
        }
        if (queue[i] == '-') {
            sum = sum - queue[i + 1];
        }
    }
    return sum;
}

console.log(calculate("玖拾玖加捌拾捌减柒拾柒加壹拾减壹拾陆加零"));